<template>
  <div class="table-page">
    <GiTable title="测试任务管理" row-key="id" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.name" placeholder="请输入任务名称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-select v-model="queryForm.status" :options="task_status" placeholder="请选择状态" allow-clear style="width: 150px"
          @change="search" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['task:info:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['task:info:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #status="{ record }">
        <GiCellTag :value="record.status" :dict="task_status" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['task:info:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['task:info:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['task:info:delete']" status="danger" :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'" @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <InfoAddModal ref="InfoAddModalRef" @save-success="search" />
    <InfoDetailDrawer ref="InfoDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import InfoAddModal from './InfoAddModal.vue'
import InfoDetailDrawer from './InfoDetailDrawer.vue'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import {
  type InfoResp,
  type InfoQuery,
  deleteInfo,
  exportInfo,
  listInfo
} from '@/apis/task/info'
import type { TableInstanceColumns } from '@/components/GiTable/type'

defineOptions({ name: 'Info' })

const { task_status } = useDict('task_status')

const queryForm = reactive<InfoQuery>({
  name: undefined,
  status: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listInfo({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  { title: '任务名称', dataIndex: 'name', slotName: 'name' },
  { title: '备注', dataIndex: 'mark', slotName: 'mark' },
  { title: '项目ID', dataIndex: 'projectId', slotName: 'projectId' },
  { title: '设备ID', dataIndex: 'deviceId', slotName: 'deviceId' },
  { title: '责任人', dataIndex: 'dutyUser', slotName: 'dutyUser' },
  { title: '参与人', dataIndex: 'participant', slotName: 'participant' },
  { title: '计划开始时间', dataIndex: 'planStartTime', slotName: 'planStartTime' },
  { title: '计划结束时间', dataIndex: 'planEndTime', slotName: 'planEndTime' },
  { title: '实际开始时间', dataIndex: 'actualStartTime', slotName: 'actualStartTime' },
  { title: '实际结束时间', dataIndex: 'actualEndTime', slotName: 'actualEndTime' },
  { title: '状态', dataIndex: 'status', slotName: 'status' },
  // { title: '文件名称', dataIndex: 'fileName', slotName: 'fileName' },
  // { title: '文件过程数据记录', dataIndex: 'fileDataRecord', slotName: 'fileDataRecord' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['task:info:detail', 'task:info:update', 'task:info:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.status = undefined
  search()
}

// 删除
const onDelete = (record: InfoResp) => {
  return handleDelete(() => deleteInfo(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportInfo(queryForm))
}

const InfoAddModalRef = ref<InstanceType<typeof InfoAddModal>>()
// 新增
const onAdd = () => {
  InfoAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: InfoResp) => {
  InfoAddModalRef.value?.onUpdate(record.id)
}

const InfoDetailDrawerRef = ref<InstanceType<typeof InfoDetailDrawer>>()
// 详情
const onDetail = (record: InfoResp) => {
  InfoDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
